//
//  RanPager.m
//  ThusMyStyle
//
//  Created by Ran on 15/9/30.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "RanPager.h"
#import "RanPagerHeaderView.h"
#import "UIView+Ran.h"

@interface RanPager()<UIScrollViewDelegate>

@property(nonatomic, weak)RanPagerHeaderView *headerView;
@property(nonatomic, weak)UIScrollView *contentView;

@property(nonatomic, strong)NSMutableArray *cacheArrayM;

@end

@implementation RanPager

#pragma mark - System

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupParams];
    }
    return self;
}

#pragma mark - Public

- (void)addHeaderViewToView:(UIView *)superView withFrame:(CGRect)headerViewFrame
{
    RanPagerHeaderView *headerView = [[RanPagerHeaderView alloc] initWithFrame:headerViewFrame];
    headerView.backgroundColor = self.headerModel.headerContainerColor;
    headerView.model = self.headerModel;
    __weak typeof(self) weakSelf = self;
    headerView.btnEventBlock = ^(NSInteger index){
        [weakSelf scrollToPage:index];
        [weakSelf showContentAtIndex: index];
    };
    [superView addSubview:headerView];
    self.headerView = headerView;
}

- (void)removeHeaderView
{
    [self.headerView removeFromSuperview];
}

- (void)addContentViewToView:(UIView *)superView withFrame:(CGRect)contentViewFrame
{
    UIScrollView *contentView = [[UIScrollView alloc] initWithFrame:contentViewFrame];
    contentView.delegate = self;
    contentView.backgroundColor = self.contentContainerColor;
    contentView.pagingEnabled = YES;
    contentView.showsHorizontalScrollIndicator = NO;
    contentView.showsVerticalScrollIndicator = NO;
    contentView.contentSize = CGSizeMake(contentViewFrame.size.width * self.headerModel.titleArray.count, contentViewFrame.size.height);
    [superView addSubview:contentView];
    self.contentView = contentView;
    [self showContentAtIndex: 0];
}

- (void)removeContentView
{
    [self.contentView removeFromSuperview];
}

#pragma mark - Delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    [self showContentAtIndex:index];
    [self.headerView scrollButtonToVisuableAtIndex:index];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    [self showContentAtIndex:index];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat value = scrollView.contentOffset.x / scrollView.frame.size.width;
    if (value < 0) {
        value = 0;
    }else if(value > self.headerModel.titleArray.count - 1){
        value = self.headerModel.titleArray.count - 1;
    }
    [self.headerView scrollBottomLineToValue:value];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
}

#pragma mark - Private

- (void)setupParams
{
    self.contentContainerColor = [UIColor whiteColor];
    self.headerModel = [[RanPagerHeaderModel alloc] init];
    self.headerModel.headerContainerColor = [UIColor whiteColor];
    self.headerModel.normalStateColor = [UIColor blackColor];
    self.headerModel.selectedStateColor = [UIColor orangeColor];
    self.headerModel.normalStateFontSize = 14;
    self.headerModel.selectedStateFontSize = 14;
    self.headerModel.headerButtonPadding = 20;
    self.headerModel.showSeperator = YES;
    self.headerModel.seperatorColor = [UIColor orangeColor];
    self.headerModel.seperatorInsets = UIEdgeInsetsMake(0, 1, 0, 1);
    self.headerModel.showBottomLine = YES;
    self.headerModel.bottomLineColor = [UIColor orangeColor];
    self.headerModel.bottomLineHeight = 1.5;
}

- (void)showContentAtIndex: (NSInteger)index
{
    if (![self.cacheArrayM containsObject:@(index)]) {
        NSLog(@"%zd", index);
        UIView *contentSubView = [self.delegate pager:self contentViewAtIndex:index];
        [self.contentView addSubview:contentSubView];
        CGFloat width = self.contentView.frame.size.width;
        CGFloat height = self.contentView.frame.size.height;
        contentSubView.frame = CGRectMake(width * index, 0, width, height);
        [self.cacheArrayM addObject:@(index)];
    }
}

- (void)scrollToPage: (NSInteger)page
{
//    CGRect rect = CGRectMake(page * self.contentView.ranWidth, 0, self.contentView.ranWidth, self.contentView.ranHeight);
//    [self.contentView scrollRectToVisible:rect animated:NO];
    CGPoint offset = self.contentView.contentOffset;
    offset.x = page * self.contentView.ranWidth;
    self.contentView.contentOffset = offset;
}

#pragma mark - Getter & Setter

- (NSMutableArray *)cacheArrayM
{
    if (!_cacheArrayM) {
        self.cacheArrayM = [NSMutableArray array];
    }
    return _cacheArrayM;
}

@end
